/**
 * <li>文件名：PurviewButtonTag.java
 * <li>说明：继承PurviewTag，增加按钮文本信息
 * <li>创建人： 曾明辉
 * <li>创建日期：2008-11-22
 * <li>修改人： 
 * <li>修改日期：
 */
package com.gp.base.tag.select;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import com.gp.base.util.StrUtil;

/**
 * <li>类型名称：
 * <li>说明：
 * <li>创建人： 曾明辉
 * <li>创建日期：2008-11-22
 * <li>修改人：
 * <li>修改日期：
 */
@SuppressWarnings("serial")
public class CommonSelectTag extends BodyTagSupport {
	
	private static final long serialVersionUID = 996559816261001992L;
	private String id;      //Select的id
	private String name;    //Select的name
	private String classes; //Select的classes
	private String style;   //Select的style
	private String disabled;//Select的style
	private String onClick; //Select的单击事件
	private String onChange;//Select的更改事件
	
	@SuppressWarnings("unchecked")
	private Collection items;//集合项
	private String optionValuePro = "value"; //选项值集合对象属性名称
	private String optionTextPro = "text";  //选项文本集合对象属性名称
	
	private String prefixOptionValue;//前缀选项值
	private String prefixOptionText; //前缀选项文本
	
	private String checkedValue;//Select的选中项的值


	public void setCheckedValue(String checkedValue) {
		this.checkedValue = checkedValue;
	}


	public void setClasses(String classes) {
		this.classes = classes;
	}


	public void setId(String id) {
		this.id = id;
	}


	public void setItems(Collection items) {
		this.items = items;
	}


	public void setName(String name) {
		this.name = name;
	}

	
	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}


	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}


	public void setOnClick(String onClick) {
		this.onClick = onClick;
	}


	public void setOptionTextPro(String optionTextPro) {
		this.optionTextPro = optionTextPro;
	}


	public void setOptionValuePro(String optionValuePro) {
		this.optionValuePro = optionValuePro;
	}


	public void setPrefixOptionText(String prefixOptionText) {
		this.prefixOptionText = prefixOptionText;
	}


	public void setPrefixOptionValue(String prefixOptionValue) {
		this.prefixOptionValue = prefixOptionValue;
	}


	public void setStyle(String style) {
		this.style = style;
	}


	@Override
	public int doStartTag() throws JspException {
		
		StringBuffer sb = new StringBuffer();
		sb.append("<select ");
		if(id != null){
			sb.append(" id=\"" + id + "\"");
		}
		if(name != null){
			sb.append(" name=\"" + name + "\"");
		}
		if(classes != null){
			sb.append(" class=\"" + classes + "\"");
		}
		if(style != null){
			sb.append(" style=\"" + style + "\"");
		}
		if(disabled != null){
			sb.append(" disabled=\"" + disabled + "\"");
		}
		if(onClick != null){
			sb.append(" onClick=\"" + onClick + "\"");
		}
		if(onChange != null){
			sb.append(" onChange=\"" + onChange + "\"");
		}
		
		sb.append(">");
		
		if(this.prefixOptionText != null){//需要显示前缀项
			if(this.prefixOptionValue == null){
				this.prefixOptionValue = "";
			}
			sb.append(this.getOptionHtml(this.prefixOptionValue, this.prefixOptionText));
		}
				
		List<String> textList = StrUtil.splitString(this.optionTextPro, "-");
		BeanWrapper bw;
		for (Object obj : items) {//显示选项
			String value = "";
			String text = "";
			if(obj instanceof String){
				value = (String)obj;
				text = (String)obj;
			}
			else{
				bw = new BeanWrapperImpl(obj);	
				value = bw.getPropertyValue(this.optionValuePro).toString();
				for (int i= 0; i < textList.size() ;i++) {
					text = text  + bw.getPropertyValue(textList.get(i)).toString();
					if(i != textList.size() -1){
						text = text + "－";
					}
				}
			}
			sb.append(this.getOptionHtml(value, text));
		}
		
		sb.append("</select>");
		
		try {
			pageContext.getOut().print(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
			throw new JspException(e.getMessage());
		}
		
		return SKIP_BODY;
	}
	
	private String getOptionHtml(Object optionValue, Object optionText){
		StringBuffer sb = new StringBuffer();
		sb.append("<option value='");
		sb.append(optionValue.toString());
		sb.append("'");
		if(this.checkedValue!=null && this.checkedValue.equals(optionValue)){
			sb.append(" selected");
		}
		sb.append(">");
		
		sb.append(optionText.toString());
		
		sb.append("</option>");
		
		return sb.toString();
		
	}
	
	
}
